package io.qifan.desktop.client.views.login;

import io.qifan.desktop.api.auth.Token;
import io.qifan.desktop.api.auth.AuthFeignClient;
import io.qifan.desktop.api.auth.request.UserLoginRequest;
import io.qifan.desktop.client.event.Events;
import io.qifan.desktop.client.views.BaseController;
import io.qifan.desktop.common.model.R;
import javafx.event.ActionEvent;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class LoginController implements BaseController {
    public TextField username;
    public PasswordField password;
    @Autowired
    private AuthFeignClient authFeignClient;

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    public void handleLogin(ActionEvent actionEvent) {
        String usernameText = username.getText();
        String passwordText = password.getText();
        UserLoginRequest userLoginRequest = new UserLoginRequest()
                .setUsername(usernameText)
                .setPassword(passwordText);
        R<Token> tokenResult = authFeignClient.login(userLoginRequest);
        log.info("登录成功，token：{}", tokenResult);
        eventPublisher.publishEvent(new Events.LoginSuccessEvent(tokenResult.getResult()));
    }
}
